package com.example.shangjinlieren.slice;

import com.example.shangjinlieren.ResourceTable;
import com.example.shangjinlieren.compontents.Swiper;
import com.example.shangjinlieren.theme.MyColors;
import com.example.shangjinlieren.utils.RpxUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.utils.Color;
import ohos.global.resource.RawFileDescriptor;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.common.Source;
import ohos.media.player.Player;
import java.util.ArrayList;
import java.util.List;
public class MainMoneyListDetailSlice extends AbilitySlice {
    static final HiLogLabel logLabel=new HiLogLabel(HiLog.LOG_APP,0x001010,"视频测试");
    private static Player player;
    private SurfaceProvider sfProvider;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main_list_detail);
        player=new Player(getContext());

//        present(new VideoAbilitySlice(),intent);
        initPage();
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    private void initPage(){
        nav();//导航
        swiper();//轮播图
        initProperInfo();//犯人信息
    }
    private void nav(){
        DirectionalLayout Id_money_list_detail =(DirectionalLayout) findComponentById(ResourceTable.Id_money_list_detail);
        ShapeElement parseshapeElement = new ShapeElement();
        parseshapeElement.setRgbColor(MyColors.themeColorGb);
        Id_money_list_detail.setBackground(parseshapeElement);
        DependentLayout componentById =(DependentLayout) findComponentById(ResourceTable.Id_money_list_nav_top_bar);
        Image Id_money_list_nav_top_bar_callback =(Image) componentById.findComponentById(ResourceTable.Id_money_list_nav_top_bar_callback);
        Id_money_list_nav_top_bar_callback.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
                //关闭视频
//                sfProvider.setVisibility(Component.HIDE);
            }
        });
        componentById.setBackground(parseshapeElement);
        componentById.setMarginTop((int) RpxUtil.vp2px(40,getContext()));
        Text Id_nav_top_bar_title =(Text) componentById.findComponentById(ResourceTable.Id_nav_top_bar_title);
        Id_nav_top_bar_title.setText("变态男");
        Id_nav_top_bar_title.setTextColor(Color.WHITE);

    }
    private void swiper(){
        List<Image> list = new ArrayList<>();
        Image image = new Image(getContext());
        image.setPixelMap(ResourceTable.Media_falv2);
        image.setWidth((int) RpxUtil.rpx(750,getContext()));
        image.setHeight((int) RpxUtil.rpx(300,getContext()));
        image.setScaleMode(Image.ScaleMode.INSIDE);
        Image image2 = new Image(getContext());
        image2.setPixelMap(ResourceTable.Media_anquanshezhi);
        image2.setWidth((int) RpxUtil.rpx(750,getContext()));
        image2.setHeight((int) RpxUtil.rpx(300,getContext()));
        image2.setScaleMode(Image.ScaleMode.INSIDE);
        list.add(image2);
        list.add(image);
        Text text = new Text(getContext());
        text.setText("123123123123123");

        new Swiper(ResourceTable.Id_money_list_detail_swiper,ResourceTable.Id_money_list_detail_swiperPageSliderIndicator, this, list);
    }
    private void initProperInfo(){
        player=new Player(getContext());
        List<Integer> listProperinfo = new ArrayList<>();
        listProperinfo.add(ResourceTable.Id_money_list_detail_swiper_proper_info1);
        listProperinfo.add(ResourceTable.Id_money_list_detail_swiper_proper_info2);
        List<Integer> listProperinfo_key = new ArrayList<>();
        listProperinfo_key.add(ResourceTable.Id_money_list_detail_swiper_proper_info_key1);
        listProperinfo_key.add(ResourceTable.Id_money_list_detail_swiper_proper_info_key2);
        List<Integer> listProperinfo_value = new ArrayList<>();
        listProperinfo_value.add(ResourceTable.Id_money_list_detail_swiper_proper_info_value1);
        listProperinfo_value.add(ResourceTable.Id_money_list_detail_swiper_proper_info_value2);
        for (int i=0;i<listProperinfo.size();i++){
            DependentLayout listProperinfoLayout =(DependentLayout) findComponentById(listProperinfo.get(i));
            Text keyLayout =(Text) findComponentById(listProperinfo_key.get(i));
            Text valueLayout =(Text) findComponentById(listProperinfo_value.get(i));
            listProperinfoLayout.setMarginTop((int) RpxUtil.rpx(30,getContext()));
            keyLayout.setPaddingLeft((int) RpxUtil.rpx(60,getContext()));
            valueLayout.setPaddingRight((int) RpxUtil.rpx(60,getContext()));
            keyLayout.setTextSize((int) RpxUtil.rpx(32,getContext()));
            valueLayout.setTextSize((int) RpxUtil.rpx(32,getContext()));
            if(i==0){
                valueLayout.setText("筑基");
                keyLayout.setText("修为");
            }else if(i==1){
                keyLayout.setText("危险程度"); valueLayout.setText("3颗星");
            }
        }

        sfProvider =(SurfaceProvider) findComponentById(ResourceTable.Id_money_list_media_player);
        sfProvider.getSurfaceOps().get().addCallback(new VideoSurfaceCallback());
        sfProvider.pinToZTop(true);
        sfProvider.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        sfProvider.setHeight((int) RpxUtil.rpx(350,getContext()));
        Image     image=(Image) findComponentById(ResourceTable.Id_paly);
        sfProvider.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                if(player.isNowPlaying()){
                    //如果正在播放，就暂停
                    player.pause();
                    //播放按钮可见
                    image.setVisibility(Component.VISIBLE);
                }else {
                    //如果暂停，点击继续播放
                    player.play();

                    //播放按钮隐藏
                    image.setVisibility(Component.HIDE);
                }
            }
        });
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        System.out.println("隐藏了");
        sfProvider.removeFromWindow();
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
    }
    private void playLocalFile(Surface surface) {

        try {
            RawFileDescriptor filDescriptor = getResourceManager().getRawFileEntry("resources/rawfile/mediamp4.mp4").openRawFileDescriptor();
            Source source = new Source(filDescriptor.getFileDescriptor(),filDescriptor.getStartPosition(),filDescriptor.getFileSize());
            player.setSource(source);
            player.setVideoSurface(surface);
            player.setPlayerCallback(new VideoPlayerCallback());
            player.prepare();
            sfProvider.setTop(0);
//            player.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private class VideoSurfaceCallback implements SurfaceOps.Callback {
        @Override
        public void surfaceCreated(SurfaceOps surfaceOps) {
            HiLog.info(logLabel,"surfaceCreated() called.");
            if (sfProvider.getSurfaceOps().isPresent()) {
                Surface surface = sfProvider.getSurfaceOps().get().getSurface();
                playLocalFile(surface);
            }
        }
        @Override
        public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {
            HiLog.info(logLabel,"surfaceChanged() called.");
        }
        @Override
        public void surfaceDestroyed(SurfaceOps surfaceOps) {
            HiLog.info(logLabel,"surfaceDestroyed() called.");
        }
    }



    private class VideoPlayerCallback implements Player.IPlayerCallback {
        @Override
        public void onPrepared() {
            HiLog.info(logLabel,"onPrepared");
        }
        @Override
        public void onMessage(int i, int i1) {
            HiLog.info(logLabel,"onMessage");
        }
        @Override
        public void onError(int i, int i1) {
            HiLog.info(logLabel,"onError: i=" + i + ", i1=" + i1);
        }
        @Override
        public void onResolutionChanged(int i, int i1) {
            HiLog.info(logLabel,"onResolutionChanged");
        }
        @Override
        public void onPlayBackComplete() {
            HiLog.info(logLabel,"onPlayBackComplete");
            if (player != null) {
                player.stop();
                player = null;
            }
        }
        @Override
        public void onRewindToComplete() {
            HiLog.info(logLabel,"onRewindToComplete");
        }
        @Override
        public void onBufferingChange(int i) {
            HiLog.info(logLabel,"onBufferingChange");
        }
        @Override
        public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {
            HiLog.info(logLabel,"onNewTimedMetaData");
        }
        @Override
        public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {
            HiLog.info(logLabel,"onMediaTimeIncontinuity");
        }

    }

}
